package com.hsy.linemoney.modules.home;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hsy.linemoney.R;

/**
 * Created by MartinKent on 2016/9/19.
 */
public class FragmentHomeRateCheckView extends RelativeLayout implements View.OnClickListener {
    private static final int duration = 300;

    private View switchButton;
    private TextView leftCopyView = null, rightCopyView = null;
    private TextView leftSpinner, rightSpinner, rateResult;
    private boolean isSwitched = false;
    private boolean isTranslateAnimating = false;
    private boolean isScaleAnimating = false;
    private AnimationListener mLocalAnimationListener;

    public FragmentHomeRateCheckView(Context context) {
        super(context);
    }

    public FragmentHomeRateCheckView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FragmentHomeRateCheckView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        switchButton = findViewById(R.id.switch_exchange_way);
        switchButton.setOnClickListener(this);
        leftSpinner = (TextView) findViewById(R.id.left_spinner);
        rightSpinner = (TextView) findViewById(R.id.right_spinner);
        rateResult = (TextView) findViewById(R.id.rate_result);
    }

    int[] loc = new int[2];
    int[] loc1 = new int[2];

    @Override
    public void onClick(View v) {
        if (isEnabled()) {
            if (!isAnimating()) {
                switchButton.setEnabled(false);
                initCopyView();

                performAnimation();
            }
        }
    }

    private boolean isAnimating() {
        return isTranslateAnimating || isScaleAnimating;
    }

    public void performAnimation() {
        if (null != mLocalAnimationListener) {
            mLocalAnimationListener.onAnimationStart();
        }
        isTranslateAnimating = true;
        isScaleAnimating = true;

        leftCopyView.clearAnimation();
        rightCopyView.clearAnimation();

        leftCopyView.setVisibility(VISIBLE);
        rightCopyView.setVisibility(VISIBLE);
        leftSpinner.setVisibility(INVISIBLE);
        rightSpinner.setVisibility(INVISIBLE);

        leftCopyView.setText(leftSpinner.getText());
        rightCopyView.setText(rightSpinner.getText());

        leftSpinner.setText(rightCopyView.getText());
        rightSpinner.setText(leftCopyView.getText());

        leftCopyView.setWidth(rightSpinner.getWidth());
        rightCopyView.setWidth(leftSpinner.getWidth());

        leftCopyView.postInvalidate();
        rightCopyView.postInvalidate();
        leftCopyView.requestLayout();
        rightCopyView.requestLayout();

        int offset = (rightCopyView.getRight() - rightCopyView.getWidth() / 2) - (leftSpinner.getRight() - leftSpinner.getWidth() / 2);//rightCopyView.getRight() - leftCopyView.getRight() + Math.abs(leftCopyView.getWidth() - rightCopyView.getWidth()) / 2;
        isSwitched = !isSwitched;

        TranslateAnimation a = new TranslateAnimation(0, offset, 0, 0);
        a.setFillAfter(false);
        a.setDuration(duration);
        a.setRepeatCount(0);
        leftCopyView.startAnimation(a);

        a = new TranslateAnimation(0, -offset, 0, 0);
        a.setFillAfter(false);
        a.setDuration(duration);
        a.setRepeatCount(0);
        a.setAnimationListener(sAnimationListener);
        rightCopyView.startAnimation(a);

        Animation anim = AnimationUtils.loadAnimation(getContext(), R.anim.home_rate_result_scale);
        anim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                isScaleAnimating = false;
                if (null != mLocalAnimationListener && !isAnimating()) {
                    mLocalAnimationListener.onAnimationEnd();

                    leftCopyView.setWidth(rightSpinner.getWidth());
                    rightCopyView.setWidth(leftSpinner.getWidth());

                    leftCopyView.invalidate();
                    rightCopyView.invalidate();
                    leftCopyView.requestLayout();
                    rightCopyView.requestLayout();
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        anim.setDuration(duration);
        rateResult.startAnimation(anim);
    }

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
        if (hasWindowFocus) {
            initCopyView();
        }
    }

    public void setAnimationListener(Animation.AnimationListener listener) {
        this.mAnimationListener = listener;
    }

    private Animation.AnimationListener mAnimationListener;

    private Animation.AnimationListener sAnimationListener = new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
            if (null != mAnimationListener) {
                mAnimationListener.onAnimationStart(animation);
            }
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            if (null != mAnimationListener) {
                mAnimationListener.onAnimationEnd(null);
            }

            leftCopyView.clearAnimation();
            rightCopyView.clearAnimation();
            leftCopyView.setVisibility(INVISIBLE);
            rightCopyView.setVisibility(INVISIBLE);
            leftSpinner.setVisibility(VISIBLE);
            rightSpinner.setVisibility(VISIBLE);

            switchButton.setEnabled(true);

            isTranslateAnimating = false;

            if (null != mLocalAnimationListener && !isAnimating()) {
                mLocalAnimationListener.onAnimationEnd();

                leftCopyView.setWidth(rightSpinner.getWidth());
                rightCopyView.setWidth(leftSpinner.getWidth());

                leftCopyView.invalidate();
                rightCopyView.invalidate();
                leftCopyView.requestLayout();
                rightCopyView.requestLayout();
            }
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            if (null != mAnimationListener) {
                mAnimationListener.onAnimationRepeat(animation);
            }
        }
    };

    private void initCopyView() {
        if (null == leftCopyView) {
            leftCopyView = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.currency_spinner_textview, null);

            getLocationOnScreen(loc);
            leftSpinner.getLocationOnScreen(loc1);

            RelativeLayout.LayoutParams params = new LayoutParams(leftSpinner.getWidth(), leftSpinner.getHeight());
            params.leftMargin = loc1[0] - loc[0];
            params.topMargin = loc1[1] - loc[1];

            addView(leftCopyView, params);

            leftCopyView.setVisibility(INVISIBLE);
        }
        if (null == rightCopyView) {
            rightCopyView = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.currency_spinner_textview, null);

            getLocationOnScreen(loc);
            rightSpinner.getLocationOnScreen(loc1);

            RelativeLayout.LayoutParams params = new LayoutParams(rightSpinner.getWidth(), rightSpinner.getHeight());
            params.leftMargin = loc1[0] - loc[0];
            params.topMargin = loc1[1] - loc[1];

            addView(rightCopyView, params);

            rightCopyView.setVisibility(INVISIBLE);
        }
    }

    public void setAnimationListener(AnimationListener listener) {
        this.mLocalAnimationListener = listener;
    }

    public boolean isSwitched() {
        return isSwitched;
    }

    public interface AnimationListener {
        void onAnimationStart();

        void onAnimationEnd();
    }
}
